package com.simple.crawler.connect.ext;

import com.simple.crawler.connect.ResponseConverter;
import com.simple.crawler.utils.IOUtils;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
public class StringResponseConverter implements ResponseConverter<String> {

    private String charSet;

    public void setCharSet(String charSet) {
        this.charSet = charSet;
    }

    @Override
    public String doConvert(InputStream in) throws Exception {
        StringBuilder sb = IOUtils.read(in, charSet, new StringBuilder());
        if (sb == null || sb.length() == 0) {
            return null;
        }
        return sb.toString();
    }

}
